package com.ourhealth.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ourhealth.constant.RedisConstant;
import com.ourhealth.entity.PageResult;
import com.ourhealth.mapper.SetMealMapper;
import com.ourhealth.pojo.CheckGroup;
import com.ourhealth.pojo.CheckItem;
import com.ourhealth.pojo.Setmeal;
import com.ourhealth.service.CheckItemGroupService;
import com.ourhealth.service.CheckItemService;
import com.ourhealth.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dddddh
 */
@Service(interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private CheckItemGroupService checkItemGroupService;
    @Autowired
    private CheckItemService checkItemService;

    @Autowired
    private JedisPool jedisPool;

    //将图片名称保存到Redis
    @Override
    public void savePic2Redis(String pic){
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,pic);
    }

    @Override
    public List<Setmeal> findAll() {
        return setMealMapper.findAll();
    }

    @Override
    public Setmeal findById(Integer id) {
        List<Integer> groupIdBySetmealIds = findGroupIdBySetmealId(id);
        List<CheckGroup> checkGroups = new ArrayList<>();
        List<CheckItem> checkItems = new ArrayList<>();
        //遍历检查组id
        for (Integer groupIdBySetmealId : groupIdBySetmealIds) {
            //通过组id查询组
            CheckGroup checkGroup = checkItemGroupService.findCheckGroupById(groupIdBySetmealId);
            //通过组id查询检查项id
            List<Integer> checkItemGroupIds = checkItemGroupService.findCheckItemIdByCheckItemGroupId(groupIdBySetmealId);
                for (Integer checkItemGroupId : checkItemGroupIds) {
                    CheckItem checkItem = checkItemService.findCheckItemById(checkItemGroupId);
                    checkItems.add(checkItem);
                }
            //将检查到的项加入检查到的组
            checkGroup.setCheckItems(checkItems);
            checkGroups.add(checkGroup);
        }
        Setmeal setmeal = setMealMapper.findById(id);
        //将检查到的组加入检查到的套餐
        setmeal.setCheckGroups(checkGroups);
        return setmeal;
    }



    @Override
    public List<Integer> findGroupIdBySetmealId(Integer id) {
        return setMealMapper.findGroupIdBySetmealId(id);
    }

    @Override
    public Setmeal findByIdUp(Integer id) {
        Setmeal setmeal = setMealMapper.findById(id);
        if(null != setmeal){
            //查询组
            List<CheckGroup> checkGroups = setMealMapper.findGroupAndSetmealBySetmealId(id);
                //通过组id查询检查项
            if(CollectionUtil.isNotEmpty(checkGroups)){
                //把检查组id放入集合，作为批量查询条件
                List<Integer> checkGroupIds = new ArrayList<>();
                for (CheckGroup checkGroup : checkGroups) {
                        checkGroupIds.add(checkGroup.getId());
                    }
                List<CheckItem> checkItems = setMealMapper.findItemAndGroupUp(checkGroupIds);
                System.out.println(checkItems);
                //索引值为checkGroupId，值为checkItem链表
                Map<Integer,List<CheckItem>> collect = new HashMap<>();
                //向map插入值
                for (CheckItem checkItem : checkItems) {
                    //mysql会把groupId相同的优先查出
                    Integer integer = checkItem.getCheckGroupId();
                    List<CheckItem> checkItemList = collect.get(integer);
                    if (checkItemList==null){
                        checkItemList = new ArrayList<>();
                        collect.put(checkItem.getCheckGroupId(),checkItemList);
                    }
                    checkItemList.add(checkItem);
                }
                //通过组id将检查项值赋给检查组
                for (CheckGroup checkGroup : checkGroups) {
                    checkGroup.setCheckItems(collect.get(checkGroup.getId()));
                }
            }
            setmeal.setCheckGroups(checkGroups);
        }
             return setmeal;
    }

    @Override
    public void addSetMeal(Setmeal setmeal) {
        setMealMapper.addSetMeal(setmeal);
        addSetMealAndCheckGroup(setmeal);
    }

    @Override
    public void addSetMealAndCheckGroup(Setmeal setmeal) {
        List<Map> list = new ArrayList<>();
        if (setmeal.getIds().length!=0){
            for (int i = 0; i < setmeal.getIds().length; i++) {
                Map<String,Integer> map = new HashMap<>();
                map.put("setMealId",setmeal.getId());
                map.put("checkGroupId",setmeal.getIds()[i]);
                list.add(map);
            }
        }
        setMealMapper.addSetMealAndCheckGroup(list);
        savePic2Redis(setmeal.getImg());
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setMealMapper.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }


}
